package com.webbei.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import com.webbei.Backstage.service.BackstateSer;
import com.webbei.HAO.service.ChSer;
import com.webbei.custom.service.CustomSer;
import com.webbei.pojo.BgPic;
import com.webbei.pojo.Brands;
import com.webbei.pojo.Products;

public class VelocityUtil {
	
	public static void productHtml(int productId,ChSer ser,CustomSer cs, BackstateSer bao){
		List<BgPic> bgpiclist= cs.getBgPicByProId(productId);
		List<Brands> list = ser.list();
		List<BgPic> pics = bao.getBgPicByProductId(productId);
		 Products product = bao.getProductById(productId);
		String bgPic = null;
		if(pics.size()>0){
		   bgPic = pics.get(0).getPicUrl();
		   System.out.println("bgPic:"+bgPic);
		}
		
		try {
			VelocityContext context = new VelocityContext();
			context.put("bgpiclist", bgpiclist);
			context.put("list", list);
			context.put("bgPic",bgPic);
			context.put("product",product);
			Template temp = Velocity.getTemplate("product.html");
			String realPath = ServletActionContext.getServletContext().getRealPath("/");
			File file = new File(realPath+productId+".shtml");
			if(!file.getParentFile().exists())file.getParentFile().mkdirs();
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
			BufferedWriter writer = new BufferedWriter(osw);
			temp.merge(context, writer);
			writer.flush();
			writer.close();
			System.out.println("velocity......");
			} catch (Exception e) {
				e.printStackTrace();
			}
	}

}
